package com.cqupt.controller;

import com.cqupt.pojo.OperationLogEntity;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.LogService;
import com.cqupt.service.UserService;
import com.cqupt.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("opreationLog")
@Controller
@ResponseBody
public class OperationLogController {

    @Autowired
    private LogService logService;

    @Autowired
    private UserService userService;
    /**
     * 获取所有的审计操作日志
     * @param cur_page 当前页码
     * @param page_size 页码大小
     * @return
     */
    @RequestMapping("getAllOperationLogs")
    public ResponseMessage getAllOperationLogs(@RequestParam(value = "currentPage") String cur_page,
                                               @RequestParam(value = "pageSize") String page_size){
        // 获取所有审计日志列表
        List<OperationLogEntity> logList = logService.getAllOperationLogs();
        // 日志总数
        int totalNum = logList.size();
        // 当前页
        int curPage = Integer.parseInt(cur_page);
        // 页面大小
        int pageSize = Integer.parseInt(page_size);
        int total = logList.size();
        int max = curPage * pageSize;
        // 分页操作
        List<OperationLogEntity> resList = new ArrayList<>();
        if (max <= total) {
            // 未越界，包含最后一页正好显示完
            for (int i = (curPage - 1) * pageSize; i < curPage * pageSize; i++) {
                resList.add(logList.get(i));
            }
        } else {
            // 最后一页不完整或越界(返回最后一页数据)
            for (int i = (total/pageSize) * pageSize; i < total; i++) {
                resList.add(logList.get(i));
            }
        }
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("logList",resList);
        resMap.put("totalNum",totalNum);
        responseMessage.setStatus(true);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 日志开关
     * @param request
     * @return
     */
    @RequestMapping("logSwitchClick")
    public ResponseMessage logSwitchClick(HttpServletRequest request){
        HttpSession session = request.getSession();
        // 从session中获取开关状态 logSwitch：true/false
        String logStatus = (String) session.getAttribute("logSwitch");
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        // 若开关状态为 true
        if (logStatus.equals("true")){
            session.setAttribute("logSwitch","false");
            userService.setUserLogStatus((String) session.getAttribute("user_id"), "false");
            resMap.put("logSwitch","false");
        }else {
            // 若开关状态为 false
            session.setAttribute("logSwitch","true");
            userService.setUserLogStatus((String) session.getAttribute("user_id"), "true");
            resMap.put("logSwitch","true");
        }
        responseMessage.setContent(resMap);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 日志搜索功能
     * @param cur_page
     * @param page_size
     * @return
     */
    @RequestMapping("logSearch")
    public ResponseMessage logSearchText(@RequestParam(value = "currentPage") String cur_page,
                                         @RequestParam(value = "pageSize") String page_size,
                                         @RequestParam(value = "searchText", required = false, defaultValue = "default") String search_text,
                                         @RequestParam(value = "startDate", required = false, defaultValue = "default") String startDate ,
                                         @RequestParam(value = "endDate", required = false, defaultValue = "default") String endDate ,
                                         @RequestParam(value = "level", required = false, defaultValue = "default") String level,
                                         @RequestParam(value = "objectModule", required = false, defaultValue = "default") String objectModule){
        // 判断时间
        if (!endDate.equals("default") || !startDate.equals("default")){
            startDate = startDate + " 00:00:00";
            endDate = endDate + " 23:59:59";
        }

        // 获取所有审计日志列表
        List<OperationLogEntity> logList = logService.getAllOperationLogsByFilter(startDate,endDate,level,objectModule,search_text);
        System.out.println(logList);
        // 日志总数
        int totalNum = logList.size();
        // 当前页
        int curPage = Integer.parseInt(cur_page);
        // 页面大小
        int pageSize = Integer.parseInt(page_size);
        int total = logList.size();
        int max = curPage * pageSize;
        // 分页操作
        List<OperationLogEntity> resList = new ArrayList<>();
        if (max <= total) {
            // 未越界，包含最后一页正好显示完
            for (int i = (curPage - 1) * pageSize; i < curPage * pageSize; i++) {
                resList.add(logList.get(i));
            }
        } else {
            // 最后一页不完整或越界(返回最后一页数据)
            for (int i = (total/pageSize) * pageSize; i < total; i++) {
                resList.add(logList.get(i));
            }
        }
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("logList",resList);
        resMap.put("totalNum",totalNum);
        responseMessage.setStatus(true);
        responseMessage.setContent(resMap);
        return responseMessage;
    }
}
